<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门" prop="deptid">
        <el-select v-model="queryParams.deptid" placeholder="请选择" clearable>
          <el-option
            v-for="item in deptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毕业院校" prop="graduate">
        <el-input
          v-model="queryParams.graduate"
          placeholder="请输入毕业院校"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:users:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:users:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:users:export']"
        >导出
        </el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户名" align="center" prop="username"/>
      <el-table-column label="姓名" align="center" prop="realname"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == '1' ? '男' : '女' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="出生日期" align="center" prop="birthday" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所在部门" align="center" prop="deptName"/>
      <el-table-column label="岗位" align="center" prop="postName"/>
      <el-table-column label="毕业院校" align="center" prop="graduate"/>
      <el-table-column label="学历" align="center" prop="xueli">
        <template slot-scope="scope">
             <span v-if="scope.row.xueli == '1'">
               大专
             </span>
             <span v-if="scope.row.xueli == '2'">
               本科
             </span>
             <span v-if="scope.row.xueli == '3'">
               研究生
             </span>
             <span v-if="scope.row.xueli == '4'">
               博士
             </span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idcard"/>
      <el-table-column label="联系方式" align="center" prop="contact"/>
      <el-table-column label="家庭住址" align="center" prop="address"/>
      <el-table-column label="入职时间" align="center" prop="workdate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == '1' ? '正常' : '锁定' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:users:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:users:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号" prop="username">
              <el-input v-model="form.username" placeholder="请输入账号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20"
                        show-password/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="realname">
              <el-input v-model="form.realname" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="radio">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker style="width: 225px" clearable
                              v-model="form.birthday"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业院校" prop="graduate">
              <el-input v-model="form.graduate" placeholder="请输入毕业院校"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门" prop="deptid">
              <el-select v-model="form.deptid" clearable placeholder="请选择" style="width: 225px">
                <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位" prop="dutyid">
              <el-select v-model="form.dutyid" clearable placeholder="请选择岗位" style="width: 225px">
                <el-option
                  v-for="item in postOptions"
                  :key="item.postId"
                  :label="item.postName"
                  :value="item.postId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学历" prop="xueli">
              <el-select v-model="form.xueli" clearable placeholder="请选择" style="width: 225px">
                <el-option
                  v-for="item in xuelis"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证" prop="idcard">
              <el-input v-model="form.idcard" placeholder="请输入身份证"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系方式"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="家庭住址" prop="address">
              <el-input v-model="form.address" placeholder="请输入家庭住址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入职日期" prop="workdate">
              <el-date-picker style="width: 225px" clearable
                              v-model="form.workdate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select style="width: 225px" v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="头像" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUsers, getUsers, delUsers, addUsers, updateUsers} from "@/api/emp/users";
import {listDeptAll} from "@/api/system/dept";
import {getUser} from "@/api/system/user";

export default {
  name: "Users",
  data() {
    return {
      deptOptions: undefined,
      xuelis: [{
        value: '1',
        label: '大专'
      }, {
        value: '2',
        label: '本科'
      }, {
        value: '3',
        label: '研究生'
      }, {
        value: '4',
        label: '博士'
      }],
      options: [{
        value: '1',
        label: '正常'
      }, {
        value: '2',
        label: '锁定'
      }],
      radio: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      usersList: [],

      postOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        password: null,
        realname: null,
        sex: null,
        birthday: null,
        deptid: null,
        dutyid: null,
        graduate: null,
        xueli: null,
        idcard: null,
        contact: null,
        address: null,
        workdate: null,
        status: null,
        image: null,
        regdate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username:[
          { required: true, message: "账号不能为空", trigger: "blur" },
        ],
        password:[
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        realname:[
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '姓名长度必须介于 5 和 20 之间', trigger: 'blur' },
        ],
        birthday:[
          { required: true, message: "请选择出生日期", trigger: "blur" }
        ],
        graduate:[
          { required: true, message: "毕业院校不能为空", trigger: "blur" }
        ],
        deptid:[
          { required: true, message: "请选择部门", trigger: "blur" }
        ],
        dutyid:[
          { required: true, message: "请选择岗位", trigger: "blur" }
        ],
        xueli:[
          { required: true, message: "学历不能为空", trigger: "blur" }
        ],
        idcard:[
          { required: true, message: "身份证不能为空", trigger: "blur" }
        ],
        contact:[
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        address:[
          { required: true, message: "家庭住址不能为空", trigger: "blur" }
        ],
        workdate:[
          { required: true, message: "请选择入职日期", trigger: "blur" }
        ],
        status:[
          { required: true, message: "请选择状态", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree()
    this.getEmp()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listUsers(this.queryParams).then(response => {
        this.usersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      listDeptAll().then(response => {
        console.log(response.data)
        this.deptOptions = response.data
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        usersid: null,
        username: null,
        password: null,
        realname: null,
        sex: null,
        birthday: null,
        deptid: null,
        dutyid: null,
        graduate: null,
        xueli: null,
        idcard: null,
        contact: null,
        address: null,
        workdate: null,
        status: null,
        image: null,
        regdate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.usersid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【员工信息】";
    },
    getEmp(){
      getUser().then(response => {
        this.postOptions = response.posts;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const usersid = row.usersid || this.ids
      getUsers(usersid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【员工信息】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sex = this.radio
          if (this.form.usersid != null) {
            updateUsers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUsers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
          this.queryParams.deptid = ""
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const usersids = row.usersid || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + usersids + '"的数据项？').then(function () {
        return delUsers(usersids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/users/export', {
        ...this.queryParams
      }, `users_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
